package com.example.cofeed.push;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.SPUtils;
import com.example.cofeed.MainActivity;
import com.example.cofeed.activity.DetailsActivity;
import com.example.cofeed.activity.LoginActivity;
import com.example.cofeed.activity.MyMsgActivity;
import com.example.cofeed.activity.ReplyCommentsActivity;
import com.example.cofeed.activity.SystemMessagesActivity;
import com.example.cofeed.activity.WebViewActivity;
import com.example.cofeed.constant.IntentKeyConstant;
import com.example.cofeed.constant.SputilsConstant;
import com.iflytek.cloud.ErrorCode;
import com.xiaomi.mipush.sdk.MiPushClient;
import com.xiaomi.mipush.sdk.MiPushCommandMessage;
import com.xiaomi.mipush.sdk.MiPushMessage;
import com.xiaomi.mipush.sdk.PushMessageReceiver;

import java.util.List;

/**
 * 小米推送
 */
public class DemoMessageReceiver extends PushMessageReceiver {
    private String mRegId;
    private long mResultCode = -1;
    private String mReason;
    private String mCommand;
    private String mMessage;
    private String mTopic;
    private String mAlias;
    private String mUserAccount;
    private String mStartTime;
    private String mEndTime;

    @Override
    public void onReceivePassThroughMessage(Context context, MiPushMessage message) {
        mMessage = message.getContent();
        if (!TextUtils.isEmpty(message.getTopic())) {
            mTopic = message.getTopic();
        } else if (!TextUtils.isEmpty(message.getAlias())) {
            mAlias = message.getAlias();
        } else if (!TextUtils.isEmpty(message.getUserAccount())) {
            mUserAccount = message.getUserAccount();
        }
    }

    @Override
    public void onNotificationMessageClicked(Context context, MiPushMessage message) {
        mMessage = message.getContent();
        String type = message.getExtra().get("type");
        String id = message.getExtra().get("id");
        String url = message.getExtra().get("url");
        if (ObjectUtils.isNotEmpty(type)) {
            if (type.equals("arcitle")) {
                Intent intent = new Intent(context, WebViewActivity.class);
                intent.putExtra(IntentKeyConstant.ID, id);
                intent.putExtra(IntentKeyConstant.URL, url);
                intent.putExtra(IntentKeyConstant.TYPE, -2);

                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);

            }
            if (ObjectUtils.isEmpty(SPUtils.getInstance().getString(SputilsConstant.TOKEN))) {
                ActivityUtils.startActivity(LoginActivity.class);
                return;
            }
            Intent intent = null;
            switch (type) {
                //商机消息
                case "askmsg":
                    intent = new Intent(context, ReplyCommentsActivity.class);
                    intent.putExtra(IntentKeyConstant.ID, id);
                    intent.putExtra(IntentKeyConstant.URL, url);
                    intent.putExtra(IntentKeyConstant.TYPE, 2);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    context.startActivity(intent);
                    break;
                //咨询回复
                case "replymsg":
                    intent = new Intent(context, ReplyCommentsActivity.class);
                    intent.putExtra(IntentKeyConstant.ID, id);
                    intent.putExtra(IntentKeyConstant.URL, url);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    context.startActivity(intent);
                    break;
                //系统消息
                case "sysmsg":
                    intent = new Intent(context, SystemMessagesActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    context.startActivity(intent);
                    break;
                //评论回复
                case "sysreply":
                    intent = new Intent(context, ReplyCommentsActivity.class);
                    intent.putExtra(IntentKeyConstant.ID, id);
                    intent.putExtra(IntentKeyConstant.URL, url);
                    intent.putExtra(IntentKeyConstant.TYPE, 1);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    context.startActivity(intent);
                    break;
                case "sms":
                    Intent i = new Intent(context, DetailsActivity.class);
//					i.putExtras(bundle);
                    String smsid = message.getExtra().get("smsid");

                    i.putExtra(IntentKeyConstant.MSG_ID, smsid);
                    //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(i);
                    break;

            }

        }

//        if (type == 1 && !ObjectUtils.isEmpty(message.getExtra().get("smsid"))) {
//            //打开自定义的Activity
////            Intent i = new Intent(context, DetailsActivity.class);
////            i.putExtra(SputilsConstant.MSG_ID, message.getExtra().get("smsid"));
////            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
////            context.startActivity(i);
//        } else if (type == 2) {
//            Intent i = new Intent(context, MyMsgActivity.class);
//            //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            context.startActivity(i);
//        } else {
//            Intent i = new Intent(context, MainActivity.class);
////            i.putExtras(bundle);
//            //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
//            context.startActivity(i);
//        }

        if (!TextUtils.isEmpty(message.getTopic())) {
            mTopic = message.getTopic();
        } else if (!TextUtils.isEmpty(message.getAlias())) {
            mAlias = message.getAlias();
        } else if (!TextUtils.isEmpty(message.getUserAccount())) {
            mUserAccount = message.getUserAccount();
        }
    }

    @Override
    public void onNotificationMessageArrived(Context context, MiPushMessage message) {
        mMessage = message.getContent();
        if (!TextUtils.isEmpty(message.getTopic())) {
            mTopic = message.getTopic();
        } else if (!TextUtils.isEmpty(message.getAlias())) {
            mAlias = message.getAlias();
        } else if (!TextUtils.isEmpty(message.getUserAccount())) {
            mUserAccount = message.getUserAccount();
        }
    }

    @Override
    public void onCommandResult(Context context, MiPushCommandMessage message) {
        String command = message.getCommand();
        List<String> arguments = message.getCommandArguments();
        String cmdArg1 = ((arguments != null && arguments.size() > 0) ? arguments.get(0) : null);
        String cmdArg2 = ((arguments != null && arguments.size() > 1) ? arguments.get(1) : null);
        if (MiPushClient.COMMAND_REGISTER.equals(command)) {
            if (message.getResultCode() == ErrorCode.SUCCESS) {
                mRegId = cmdArg1;
                SPUtils.getInstance().put(SputilsConstant.XIAOMI_TOKEN, mRegId);
            }
        } else if (MiPushClient.COMMAND_SET_ALIAS.equals(command)) {
            if (message.getResultCode() == ErrorCode.SUCCESS) {
                mAlias = cmdArg1;
            }
        } else if (MiPushClient.COMMAND_UNSET_ALIAS.equals(command)) {
            if (message.getResultCode() == ErrorCode.SUCCESS) {
                mAlias = cmdArg1;
            }
        } else if (MiPushClient.COMMAND_SUBSCRIBE_TOPIC.equals(command)) {
            if (message.getResultCode() == ErrorCode.SUCCESS) {
                mTopic = cmdArg1;
            }
        } else if (MiPushClient.COMMAND_UNSUBSCRIBE_TOPIC.equals(command)) {
            if (message.getResultCode() == ErrorCode.SUCCESS) {
                mTopic = cmdArg1;
            }
        } else if (MiPushClient.COMMAND_SET_ACCEPT_TIME.equals(command)) {
            if (message.getResultCode() == ErrorCode.SUCCESS) {
                mStartTime = cmdArg1;
                mEndTime = cmdArg2;
            }
        }
    }

    @Override
    public void onReceiveRegisterResult(Context context, MiPushCommandMessage message) {
        String command = message.getCommand();
        List<String> arguments = message.getCommandArguments();
        String cmdArg1 = ((arguments != null && arguments.size() > 0) ? arguments.get(0) : null);
        String cmdArg2 = ((arguments != null && arguments.size() > 1) ? arguments.get(1) : null);
        if (MiPushClient.COMMAND_REGISTER.equals(command)) {
            if (message.getResultCode() == ErrorCode.SUCCESS) {
                mRegId = cmdArg1;
            }
        }
    }
}